package com.fucking.great.context.plun;

import cn.hutool.core.thread.ThreadUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.embedded.LinkedHashMapCacheBuilder;
import com.fucking.great.context.config.TxnConfiger;
import com.fucking.great.context.util.GetMapDecorator;
import com.fucking.great.context.util.SingleUtil;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用于调用方法.
 */
public class CachePlug extends BasePlug{
    public static final String PLUN_NAME = "cache";

    public CachePlug(Map<String, String> config) {
        super( config,new String[]{
                Cache.class.getName()
        });
    }

    @Override
    protected void initUserPlunMap(Map<String, Object> userPlun) {
        GetMapDecorator<String, String> cacheConfig = this.getPrefixConfig("cache");
        Integer limit = cacheConfig.getInt("limit", 1000);
        String timeUnit = cacheConfig.getStr("timeUnit","SECONDS");
        Long defaultExpire = cacheConfig.getLong("defaultExpire", 600l);
        //
        Cache<String, Object> cache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder()
                .limit(limit)
                .expireAfterWrite(defaultExpire, TimeUnit.valueOf(timeUnit) )
                .buildCache();
        //
        userPlun.put(PLUN_NAME,cache);
    }



}
